package com.xk39.mybatisplusdemo;



import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

public class CodeGenerator {
    
    public static String scanner(String tip){
        Scanner scanner = new Scanner(System.in);
        StringBuilder builder = new StringBuilder();
        builder.append("请输入"+tip+": ");
        System.out.println(builder.toString());
        if(scanner.hasNext()){
            String next = scanner.next();
            if(StringUtils.isNotBlank(next)){
                return next;
            }
        }
        throw new MybatisPlusException("请输入正确的"+tip+"！");
    }
    
    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator();
        
        
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setFileOverride(true);
        gc.setOpen(false);
        gc.setAuthor("xk39");
        gc.setIdType(IdType.AUTO);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        gc.setServiceName("%sService");
        generator.setGlobalConfig(gc);
    
    
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/" +
                "mp?useSSL=false&useUnicode=true&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("xk_root");
        generator.setDataSource(dsc);
    
    
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.xk39.mybatisplusdemo");
        pc.setMapper("mapper");
        pc.setXml("mapper.xml");
        pc.setEntity("pojo");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        generator.setPackageInfo(pc);
    
    
        StrategyConfig sc = new StrategyConfig();
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true);
        sc.setRestControllerStyle(true);
        sc.setControllerMappingHyphenStyle(true);
        sc.setTablePrefix("tbl_");
        sc.setInclude(scanner("表名,多个英文逗号分割").split(","));
        generator.setStrategy(sc);
        
        generator.execute();
    }
    
}
